<template>
  <div class="rbt-checkbox" @click="changeChecked">
    <i v-if="checked" class="iconfont icon-checked"></i>
    <i v-else class="iconfont icon-unchecked"></i>
    <span v-if="$slots.default"><slot /></span>
  </div>
</template>

<script setup lang="ts">
const props = defineProps({
  modelValue: {
    type: Boolean,
    default: false
  }
})
const emits = defineEmits<{
  (e: 'update:modelValue', value: boolean): void
  (e: 'change', value: boolean): void
}>()
const checked = ref<boolean>(false)
const changeChecked = () => {
  checked.value = !checked.value
  emits('update:modelValue', checked.value)
  emits('change', checked.value)
}

watch(
  () => props.modelValue,
  () => {
    checked.value = props.modelValue
  },
  { immediate: true }
)
</script>

<style scoped lang="scss">
.rbt-checkbox {
  display: inline-block;
  margin-right: 2px;
  cursor: pointer;
  .icon-checked {
    color: $rbtColor;
    ~ span {
      color: $rbtColor;
    }
  }
  i {
    position: relative;
    top: 1px;
  }
  span {
    margin-left: 2px;
  }
}
</style>
